05. Exercise: Add a RecyclerView

L7 04 Implementing An Adapter SC

Now it's your turn to complete this exercise yourself!

NOTE: This lesson is a continuation of the code you completed in Lesson 6, and includes additional code that you’ll need to complete this lesson. To get started, make sure you clone the GitHub repository and check out the starter-code branch, or you can download the zip File.

This exercise in completed in two parts. After this part, there won't be any data on the screen, but have no fear, that's coming right up!

In this first exercise, you will replace your main ScrollView with a RecyclerView, then implement an Adapter for displaying list items. There are quite a few steps, but when you're done, you'll have a working app that uses a RecyclerView to display data.

  1. Add RecyclerView to layout.
    Open fragment_sleep_tracker.xml and replace the entire ScrollView, including the enclosed TextView, with a RecyclerView:
 <androidx.recyclerview.widget.RecyclerView
     android:id="@+id/sleep_list"
     android:layout_width="0dp"
     android:layout_height="0dp"
     app:layout_constraintBottom_toTopOf="@+id/clear_button"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@+id/stop_button"
     app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

You should be able to see that it's constrained correctly in the Design tab. The RecyclerView should fill all the space between the buttons. There should not be a ScrollView anymore.

In SleepNightAdapter.kt

  1. Create the SleepNightAdapter class.

    The class should extend RecyclerView.Adapter and hold a TextItemViewHolder.

 class SleepNightAdapter: RecyclerView.Adapter<TextItemViewHolder>() 

The code won’t compile yet, since you haven’t implemented all of the required overrides yet. We'll do that below.


  1. Define a data source.
    Create variable data and assign it to listOf<SleepNight>.


  2. Override getItemCount() and have it return data.size.


  3. Override onBindViewHolder()

    The function should retrieve the item from the data list, and set holder.textView.text to item.sleepQuality.toString().


  4. Override onCreateViewHolder().

    NOTE The Adapter requires this method, but we won't fill it in here. We’ll cover it in a later exercise, but for now it just make sure it contains the line, TODO("not implemented"), so the code will compile.


  5. Verify that the code compiles and runs without errors.

    NOTE You should be able to click START and STOP, and assign sleep quality, but items will not appear on the screen yet because we’ve removed the ScrollView. In the next exercise, you'll display data in the RecyclerView.

If you want to start at this step, you can download this exercise from: Step.01-Exercise-Add-a-RecyclerView.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here: Step.01-Solution-Add-a-RecyclerView, or using this git diff.

Task Description:

Complete these tasks to implement a RecyclerView.

Task List:

Task Feedback:

Great job! Next you'll finish the Adapter interface with onCreateViewHolder() and tell RecyclerView how to use your adapter to display a list of SleepNight.

Reference Documentation